class PubSub {
    message: { [key: string]: any[] }
    constructor() {
        this.message = {}
    }
    // 发布
    publish(type: string, ...args: any[]): void {
        if (this.message[type]?.length >= 0) {
            this.message[type].forEach(item => item(...args))
        }
    }
    // 卸载订阅
    unsubsribe(type: string, func?: any) {
        if (this.message[type]) {
            if (func) {
                this.message[type] = this.message[type].filter(item => item !== func)
            } else {
                delete this.message[type]
            }
        }
    }
    // 订阅
    subscribe(type: string, func: any) {
        if (!this.message[type]) {
            this.message[type] = []
        }
        this.message[type].push(func)
    }
}

export default PubSub